<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/9/3
 * Time: 上午8:04
 */

namespace app\api\module;


use app\common\model\DeveloperException;
use app\common\model\User;
use app\common\model\Chat as ChatModel;
use app\common\model\UserException;

class Chat extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
            return true;
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return string
     * @throws DeveloperException
     */
    public function exec($interface)
    {
        switch ($interface)
        {
            /**
             * @api {post} / 【1001】对话列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName talklist
             * @apiGroup 私信【Chat】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Chat",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "page":"1",
             *     "page_size":"1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} list 对话列表
             *
             * @apiSuccess (union_list-消息列表) {String} create_time 提现时间
             * @apiSuccess (union_list-消息列表) {String} money 提现金额
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "list": [
             *       {
             *             "create_time":"123456,
             *             "money":"1.23"
             *       },
             *       {
             *             "create_time":"123456,
             *             "money":"1.23"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "提现列表！"
             * }
             *
             */
            case H_CHAT_TALKLIST:
                return $this->chat_talklist();
            /**
             * @api {post} / 【1002】进行对话
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName message
             * @apiGroup 私信【Chat】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} getid 对话对方会员ID
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Chat",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "getid":"1"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} list 对话详情
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "list": [
             *       {
             *             "uinfo":"[{}],
             *             "chat_maninfo":[{}],
             *             "chat_list":[{}]
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "对话详情！"
             * }
             *
             */
            case H_CHAT_MESSAGE:
                return $this->chat_message();
            case H_CHAT_SAVEMESSAGE:
                return $this->chat_savemessage();
            case H_CHAT_LOADMESSAGE:
                return $this->chat_loadmessage();
            case H_CHAT_HISTORY:
                return $this->chat_history();
            default:
                send_developer_error("模块【活动资源】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }



    /*
     * 对话列表
     * */
    public function chat_talklist()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $user=User::userFromToken($access_token);

        $list=ChatModel::talkList($user,$page,$page_size);
        $this->insertResponse('list',$list);
        return $this->response("对话列表！");

    }


    /*
    * 进行对话
    * */
    public function chat_message()
    {
        $access_token=$this->essentialParam('access_token');
        $getid=$this->essentialParam('getid');

        $user=User::userFromToken($access_token);

        $list=ChatModel::message($user,$getid);
        $this->insertResponse('list',$list);
        return $this->response("对话详情！");

    }

    /*
    * 保存对话
    * */
    public function chat_savemessage()
    {
        $access_token=$this->essentialParam('access_token');
        $getid=$this->essentialParam('getid');
        $content=$this->essentialParam('content');

        $user=User::userFromToken($access_token);

        $result=ChatModel::savemessage($user,$getid,$content);
        $this->insertResponse('result',$result);
        return $this->response("保存对话！");

    }

    /*
    * 拉取对话
    * */
    public function chat_loadmessage()
    {
        $access_token=$this->essentialParam('access_token');
        $getid=$this->essentialParam('getid');
        $lastid=$this->essentialParam('lastid');

        $user=User::userFromToken($access_token);

        $list=ChatModel::loadmessage($user,$getid,$lastid);
        $this->insertResponse('list',$list);
        return $this->response("拉取对话！");

    }

    /*
     * 对话历史
     * */
    public function chat_history()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $getid=$this->essentialParam('getid');
        $user=User::userFromToken($access_token);

        $list=ChatModel::history($user,$getid,$page,$page_size);
        $this->insertResponse('list',$list);
        return $this->response("对话历史！");

    }

}